<?php
class View_Recommendation{

	public static function dispalyRecommendations($arrRecommendation, $idViewer, $idEssence, $essenceType){
		$autor = Service_Essence::getAuthor($idEssence, $essenceType);
		$txtReturn = '<div id="allRecommendation">';
		foreach ($arrRecommendation as $arrOneRecommendation){
			if ($arrOneRecommendation['hide_flag'] == 0 OR $idViewer == $autor OR $idViewer == $arrOneRecommendation['ID_user']){
				$showConnectButton = true;

				$txtReturn .=  '<ul class="recommendationsList">';
					$txtReturn .=  '<li id="' . $arrOneRecommendation['ID_recommendation'] . '_recommendation">';
						$txtReturn .=  '<span class="mdash weight">&mdash;</span>';
						$txtReturn .=  '<span class="weight text"> <i>' . $arrOneRecommendation['text'] . '</i></span>';

						if ($idViewer == $autor)//если $idViewer == $autor выводим ссылки управления рекомендацией
						 $txtReturn .= self::getRecommendationsAutorEdit($arrOneRecommendation, $essenceType, $idEssence);

						$txtReturn .=  '<p class="grey">';//вывод подтвердивших пользователей
							if (!empty($arrOneRecommendation['users']))
							$i = 0;
							$size = sizeof($arrOneRecommendation['users']);
								foreach ($arrOneRecommendation['users'] as $arr){
									$i++;
									if ($idViewer == $arr['ID_user'])
										$showConnectButton = false;
									$txtReturn .=  ' <a class=' . ($arrOneRecommendation['ID_user'] == $arr['ID_user'] ? '"blue" title="автор рекоммендации"' : '"black"') . ' href="' . W_ABSPATH . 'user/' . $arr['login'] . '">' . $arr['login'] . '</a><span title="рейтинг" class="grey">[' . round($arr['rating'], 2) . ']</span>' . ($i < $size ? ', ' : ' ');
								}
							if ($idViewer != $autor)
								$txtReturn .=  '&nbsp;<a class="green" href="javascript:recommendation.connect(' . $arrOneRecommendation['ID_recommendation'] . ', ' . ($showConnectButton ? 1 : 0) . ', ' .  $essenceType . ', ' . $idEssence . ')">' . ($showConnectButton ? 'Присоединиться' : 'Отказаться') . '</a>';
						$txtReturn .=  '</p>';

					$txtReturn .=  '</li>';
				$txtReturn .=  '</ul>';
			}
		}

		if (empty($arrRecommendation))
			$txtReturn .=  '<p>Рекомендаций ещё нет</p>';

		if ($idViewer != $autor)
			$txtReturn .= self::getRecommendationsAddForm($essenceType, $idEssence);
		$txtReturn .= '</div>';
		return $txtReturn;
	}

	/**
	 * сслки управления для автора
	 */
	public static function getRecommendationsAutorEdit($arrOneRecommendation, $essenceType, $idEssence){
		$txtReturn = '';
		if ($arrOneRecommendation['hide_flag'] == 0)
			$txtReturn .=  '<span id="' . $arrOneRecommendation['ID_recommendation'] . '_recommendationStatus">
							<a class="greyJs hide" href="javascript:recommendation.show(' . $arrOneRecommendation['ID_recommendation'] . ', ' . $essenceType . ', ' . $idEssence . ', 1)">скрыть</a>&nbsp;
						</span>';
		else
			$txtReturn .=  '<span id="' . $arrOneRecommendation['ID_recommendation'] . '_recommendationStatus">
							<span class="hide">Скрыто, видно только вам и автору</span>
							<a class="greyJs hide" href="javascript:recommendation.show(' . $arrOneRecommendation['ID_recommendation'] . ', ' . $essenceType . ', ' . $idEssence . ', 0)">показать</a>&nbsp;
						</span>';
			$txtReturn .=  '<a class="greyJs" href="javascript:if(confirm(\'Вы действительно хотите удалить?\'))recommendation.deleteRecommendation(' . $arrOneRecommendation['ID_recommendation'] . ', ' . $essenceType . ', ' . $idEssence . ')">удалить</a></span>';

			return $txtReturn;
	}

	/**
	 * форма добавления
	 */
	public static function getRecommendationsAddForm($essenceType, $idEssence){
			$txtReturn =  '<a class="js addRecommendation" href="" onClick="recommendation.toggleAddRecommendation(); return false;">Добавить рекомендацию</a>';
			$txtReturn .=  '<form id="recommendationAddForm" style="display: none;" onSubmit="recommendation.saveRecommendation(' . $essenceType . ', ' . $idEssence . '); return false;">';
				$txtReturn .=  '<h3>Добавить рекомендацию</h3>';
				$txtReturn .=  '<textarea name="recommendationMessage" id="text" class="recommendationMessage" cols="52" rows="10"></textarea>';
				$txtReturn .=  '<p><input type="image" src="' . W_IMAGES . 'button/btn_main_otpravit.gif"/> <a class="js" id="addRecommendationClose" href="javascript:recommendation.toggleAddRecommendation();">закрыть</a></p>';
			$txtReturn .=  '</form>';

			return $txtReturn;
	}
}
?>